<?php

use App\Http\Controllers\admin\DictionaryController;
use App\Http\Controllers\admin\GroupController;
use App\Http\Controllers\admin\PermissionController;
use App\Http\Controllers\admin\UserController;
use App\Http\Controllers\ProjectManager\ProjectManagerController;
use App\Http\Middleware\CheckPermission;
use App\Http\Middleware\CheckGroupPermission;
use App\Http\Middleware\SetNormalParams;
use App\Http\Middleware\ValidateOwnerResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Tymon\JWTAuth\Http\Middleware\Authenticate;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

//后台管理
Route::prefix('admin')->group(function () {
    Route::post("login", [UserController::class, 'login']);
    Route::post("register", [UserController::class, 'register']);
    Route::post('logout', [UserController::class, 'logout'])->name('Logout')->middleware([Authenticate::class]);
    Route::get('user/information', [UserController::class, 'GetUserMessage'])->name('GetUserInformation')->middleware([Authenticate::class, ValidateOwnerResource::class]);
    Route::put('passwd/reset', [UserController::class, 'resetPasswd'])->middleware([Authenticate::class, ValidateOwnerResource::class]);
    //root用户可以修改所有用户的信息，普通用户只能修改自己的信息
    Route::put('change/user/message', [UserController::class, 'changeUserMessage'])->middleware([Authenticate::class, ValidateOwnerResource::class])->name('changeUserMessage');

    //使用中间件检查id，如果为1则为root用户，root用户使用一张表来管理其他用户是否可以拥有对底层功能的使用
    Route::middleware([Authenticate::class, CheckPermission::class])->prefix('permissions')->group(function () {
        //权限管理 有且仅有root用户及root用户授权的用户可以执行
        Route::post('addPermissionForUser', [PermissionController::class, 'addPermissionForUser'])->name('addPermissionForUser');
        //添加权限类型
        Route::post('add', [PermissionController::class, 'addPermission'])->name('addPermission');
        Route::delete('remove/{id}', [PermissionController::class, 'removePermission'])->name('removePermission');
        Route::get('get', [PermissionController::class, 'selectPermission'])->name('getPermission');
        Route::put('update', [PermissionController::class, 'updatePermission'])->name('updatePermission');
        Route::get('getRouteNames', [PermissionController::class, 'getRouteNames'])->name('getRouteNames');
    });

    //组的管理
    Route::middleware([Authenticate::class, CheckPermission::class])->prefix('group')->group(function () {

        //任何用户均可执行，但是只能操作自己的组
        Route::post('add', [GroupController::class, 'addGroup'])->name('CreateGroup');
        Route::delete('remove/{id}', [GroupController::class, 'removeGroup'])->name('RemoveGroup'); //y
        Route::delete('disableGroup/{id}', [GroupController::class, 'disableGroup'])->name('DisableGroup')->middleware([ValidateOwnerResource::class]); //y
        Route::put('update', [GroupController::class, 'updateGroup'])->name('UpdateGroup'); //y
        Route::get('select/user/groups', [GroupController::class, 'selectUserGroups'])->middleware([ValidateOwnerResource::class])->name('SelectUserGroups');

        //只有root用户可以执行
        Route::post('set/group/permission', [GroupController::class, 'SetGroupPermission'])->name('SetGroupPermission');
        Route::get('get', [GroupController::class, 'selectGroup'])->middleware(SetNormalParams::class)->name('GetAllGroups');
        Route::delete('remove/group/permission/{id}', [GroupController::class, 'RemoveGroupPermission']);
        Route::delete('disable/group/permission/{id}', [GroupController::class, 'disableGroupPermission']);

        //组的创建者，以及组的管理者可以执行
        Route::post('user/add', [GroupController::class, 'AddUser'])->name("AddUserInGroup"); //y
        Route::delete('user/remove', [GroupController::class, 'removeUser'])->name("RemoveUserFromGroup");//y
        //组内成员均可执行
        Route::get('get/group/permissions', [GroupController::class, 'SelectGroupPermission'])->name("GetGroupAllPermissions");
        Route::get('get/group/users', [GroupController::class, 'selectGroupUsers'])->name("GetGroupAllUsers");
    });

    //数据字典的管理 有且仅有root用户及root用户授权的用户可以执行
    Route::middleware([Authenticate::class, CheckPermission::class])->prefix('Dictionary')->group(function () {
        Route::get('get', [DictionaryController::class, 'selectDictionary'])->name('selectDictionary');
        Route::delete('remove/{id}', [DictionaryController::class, 'removeDictionary'])->name('removeDictionary');
        Route::put('update', [DictionaryController::class, 'updateDictionary'])->name('updateDictionary');
        Route::post('add', [DictionaryController::class, 'addDictionary'])->name('addDictionary');
    });
});

Route::prefix('projectmanager')->middleware([Authenticate::class, CheckPermission::class])->group(function () {
    Route::post("add/project/message/item", [ProjectManagerController::class, "addItem"])->name("AddProjectManagerItem");
    Route::get("select/project/message/item", [ProjectManagerController::class, "SelectStoreItems"])->name("SelectProjectManagerItem");
    Route::get('get/user/ide/types', [ProjectManagerController::class, "getIdeTypes"])->name("GetUserIdeTypes");
    Route::put('update/project/item', [ProjectManagerController::class, "UpdateItem"])->name("UpdateProjectItem");
    Route::delete('delete/project/item/{id}', [ProjectManagerController::class, "DeleteItem"])->name("DeleteProjectItem");
    Route::get('get/user/project/message', [ProjectManagerController::class, "getUserProjectMessage"])->name("GetUserProjectManagerMessage");
});

Route::post('postEmail', [UserController::class, 'sendEmail']);
Route::get('getCaptcha', [UserController::class, 'captcha']);
Route::get('checktoken', [UserController::class, 'CheckToken'])->middleware([Authenticate::class]);
